package com.example.cunion.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.cunion.common.R;
import com.example.cunion.config.shiro.JwtUtil;
import com.example.cunion.controller.form.*;
import com.example.cunion.entity.Complaint;
import com.example.cunion.service.ComplaintService;
import com.example.cunion.util.StringSnowflakeIdGenerator;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/complaint")
public class ComplaintController {
    @Resource
    private ComplaintService complaintService;

    @Resource
    private JwtUtil jwtUtil;

    @PostMapping("/searchUnCheckFirstComplaintByPage")
    public R searchUnCheckFirstComplaintByPage(@RequestHeader("token") String token, @RequestBody SearchUnCheckComplaintByPageForm form){
        Integer start = form.getStart();
        Integer length = form.getLength();
        start = (start - 1) * length;
        HashMap<String, Object> map = new HashMap<>();
        map.put("start", start);
        map.put("length", length);
        List<HashMap> list = complaintService.searchUnCheckFirstComplaintByPage(map);
        return R.ok().put("result", list);
    }
    @PostMapping("/searchUnCheckComplaintByPage")
    public R searchUnCheckComplaintByPage(@RequestHeader("token") String token, @RequestBody SearchUnCheckComplaintByPageForm form){
        Integer start = form.getStart();
        Integer length = form.getLength();
        start = (start - 1) * length;
        HashMap<String, Object> map = new HashMap<>();
        map.put("start", start);
        map.put("length", length);
        List<HashMap> list = complaintService.searchUnCheckByPage(map);
        return R.ok().put("result", list);
    }

    @GetMapping("/agreeComplaint")
    public R agreeComplaint(@RequestHeader("token") String token, @RequestParam("id") String id){
        Integer result = complaintService.agreeComplaint(id);
        return R.ok();
    }

    @GetMapping("/rejectComplaint")
    public R rejectComplaint(@RequestHeader("token") String token, @RequestParam("id") String id){
        Integer result = complaintService.rejectComplaint(id);
        return R.ok();
    }

    @PostMapping("/addComplaint")
    public R addComplaint(@RequestHeader("token") String token, @RequestBody AddComplaintForm form){
        String userId = jwtUtil.getUserId(token);
        form.setUserId(userId);
        StringSnowflakeIdGenerator generator = new StringSnowflakeIdGenerator(1, 1);
        String id = generator.nextId();
        form.setId(id);
        Map<String, Object> map = BeanUtil.beanToMap(form);
        Integer result = complaintService.addComplaint((HashMap) map);
        return R.ok();
    }

    @PostMapping("/updateComplaintStatus")
    public R updateComplaintStatus(@RequestHeader("token") String token, @RequestBody UpdateComplaintStatusForm form){
        Map<String, Object> map = BeanUtil.beanToMap(form);
        Integer result = complaintService.updateComplaintStatus((HashMap) map);
        return R.ok();
    }

    @PostMapping("/addComplaintByMp")
    public R addComplaintByMp(@RequestHeader("token") String token, @RequestBody Complaint complaint) {
        complaintService.addComplaint(complaint);
        return R.ok();
    }

    @GetMapping("/deleteComplaintByMp")
    public R deleteComplaintByMp(@RequestHeader("token") String token, @RequestParam("complaintId") String complaintId) {
        complaintService.deletedComplaint(complaintId);
        return R.ok();
    }
    @PostMapping("/updateComplaintByMp")
    public R updateComplaintByMp(@RequestHeader("token") String token, @RequestBody Complaint complaint) {
        complaintService.updateComplaint(complaint);
        return R.ok();
    }
    @PostMapping("/searchComplaintByMp")
    public R searchComplaintByMp(@RequestHeader("token") String token, @RequestBody PageFom form){
        List<Complaint> list = complaintService.searchComplaint(form.getStart(), form.getLength());
        return R.ok().put("result", list);
    }
}
